package com.canaan.business.controller.pc.api.erp;

import cn.hutool.json.JSONUtil;
import com.canaan.common.encrypt.AesCryptorUtils;
import com.canaan.common.encrypt.ApiResult;
import com.canaan.common.encrypt.ResultDTO;
import com.canaan.business.domain.basic.ErpProduct;
import com.canaan.business.domain.vo.ErpProductVo;
import com.canaan.business.service.basic.IErpProductService;
import com.canaan.common.annotation.ApiInterfaceLog;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 物料主数据Controller
 *
 * @author ruoyi
 * @date 2022-11-30
 */
@Slf4j
@Api(tags = "物料")
@RestController
public class ProductController extends BaseController {
    @Autowired
    private IErpProductService erpProductService;
    @Autowired
    private HttpCommonReq commonReq;

    /**
     * 新增物料主数据
     */
    @ApiOperation(value = "同步物料")
    @ApiInterfaceLog(title = "同步物料")
    @PostMapping("/edi/product/sync")
    public String sync(HttpServletRequest request) {
        try {
            String json = commonReq.getCommonReq(request).getData();
            ErpProductVo erpProduct = JSONUtil.toBean(json, ErpProductVo.class);
            ErpProduct product = erpProductService.selectErpProductByProductNo(erpProduct.getMidDataNode());
            ErpProduct tmp = generateErpProduct(erpProduct);
            if (erpProduct.getAction().equals("1") || erpProduct.getAction().equals("2")) {
                if (product != null) {
                    tmp.setId(product.getId());
                    tmp.setUpdateBy("ERP");
                    return buildResult(true, String.valueOf(erpProductService.updateErpProduct(tmp)));
                }
                tmp.setCreateBy("ERP");
                return buildResult(true, String.valueOf(erpProductService.insertErpProduct(tmp)));
            } else {
                throw new Exception(String.format("%s物料action不属于新增或更新", erpProduct.getMidDataNode()));
            }
        } catch (Exception e) {
            log.error("同步物料信息异常,msg:{}", e.getMessage(), e);
            return buildResult(false, e.getMessage());
        }
    }

    /**
     * 创建Erp物料
     *
     * @param vo
     * @return
     */
    private ErpProduct generateErpProduct(ErpProductVo vo) {
        ErpProduct erpProduct = new ErpProduct();
        erpProduct.setProductNo(vo.getMidDataNode());
        erpProduct.setBarCode(vo.getBarCodes());
        erpProduct.setProductName(vo.getName());
        erpProduct.setLargeCategoryName(vo.getLargeCategoryName());
        erpProduct.setMidCategoryName(vo.getMidCategoryName());
        erpProduct.setSmallCategoryName(vo.getSmallCategoryName());
        erpProduct.setBrandName(vo.getBrandName());
        erpProduct.setYlUnitName(vo.getYlUnitName());
        erpProduct.setPsUnitName(vo.getPsUnitName());
        erpProduct.setYlPsConver(vo.getYlPsConver());
        erpProduct.setCgUnitName(vo.getCgUnitName());
        erpProduct.setCgPsConver(vo.getCgPsConver());
        erpProduct.setSpecUnitName(vo.getSpecUnitName());
        erpProduct.setSpcYlConver(vo.getSpecYlConver());
        if (StringUtil.isNotEmpty(vo.getPurchaseLength())) {
            erpProduct.setPurchaseLength(new BigDecimal(vo.getPurchaseLength()));
        }
        if (StringUtil.isNotEmpty(vo.getPurchaseWidth())) {
            erpProduct.setPurchaseWidth(new BigDecimal(vo.getPurchaseWidth()));
        }
        if (StringUtil.isNotEmpty(vo.getPurchaseHeight())) {
            erpProduct.setPurchaseHeight(new BigDecimal(vo.getPurchaseHeight()));
        }
        if (StringUtil.isNotEmpty(vo.getPurchaseVolume())) {
            erpProduct.setPurchaseVolume(new BigDecimal(vo.getPurchaseVolume()));
        }
        if (StringUtil.isNotEmpty(vo.getPurchaseGrossWeight())) {
            erpProduct.setPurchaseCrossWeight(new BigDecimal(vo.getDeliveryGrossWeight()));
        }
        if (StringUtil.isNotEmpty(vo.getDeliveryLength())) {
            erpProduct.setDeliveryLength(new BigDecimal((vo.getDeliveryLength())));
        }
        if (StringUtil.isNotEmpty(vo.getDeliveryWidth())) {
            erpProduct.setDeliveryWidth(new BigDecimal(vo.getDeliveryWidth()));
        }
        if (StringUtil.isNotEmpty(vo.getDeliveryHeight())) {
            erpProduct.setDeliveryHeight(new BigDecimal(vo.getDeliveryHeight()));
        }
        if (StringUtil.isNotEmpty(vo.getDeliveryVolume())) {
            erpProduct.setDeliveryVolume(new BigDecimal(vo.getPurchaseVolume()));
        }
        if (StringUtil.isNotEmpty(vo.getDeliveryGrossWeight())) {
            erpProduct.setDeliveryGrossWeight(new BigDecimal(vo.getDeliveryGrossWeight()));
        }
        if (StringUtil.isNotEmpty(vo.getSpecLength())) {
            erpProduct.setSpecLength(new BigDecimal(vo.getSpecLength()));
        }
        if (StringUtil.isNotEmpty(vo.getSpecWidth())) {
            erpProduct.setSpecWidth(new BigDecimal(vo.getSpecWidth()));
        }
        if (StringUtil.isNotEmpty(vo.getSpecHeight())) {
            erpProduct.setSpecHeight(new BigDecimal(vo.getSpecHeight()));
        }
        if (StringUtil.isNotEmpty(vo.getSpecVolume())) {
            erpProduct.setSpecVolume(new BigDecimal(vo.getSpecVolume()));
        }
        if (StringUtil.isNotEmpty(vo.getSpecGrossWeight())) {
            erpProduct.setSpecGrossWeight(new BigDecimal(vo.getSpecGrossWeight()));
        }
        erpProduct.setStoreAcceptancePeriod(vo.getStoreAcceptancePeriod());
        erpProduct.setWarehouseAcceptancePeriod(vo.getWareHouseAcceptancePeriod());
        erpProduct.setQualityGuaranteePeriod(vo.getQualityGuaranteePeriod());
        erpProduct.setStorageTypeblb(vo.getStorageTypeblb());
        erpProduct.setFinanceAttribute(vo.getFinanceAttribute());
        erpProduct.setGoodsAttibute(vo.getGoodsAttribute());
        erpProduct.setSpecType(vo.getSpecType());
        return erpProduct;
    }

    /**
     * 生成返回结果
     *
     * @param hasSuccess
     * @return
     */
    private static String buildResult(boolean hasSuccess, String defaultMsg) {
        ApiResult<ResultDTO<AjaxResult>> result = new ApiResult<>();
        ResultDTO<AjaxResult> resultDTO = new ResultDTO<>();
        AjaxResult ajaxResult = new AjaxResult();
        if (hasSuccess) {
            resultDTO.setStatus("success");
            ajaxResult.success();
        } else {
            resultDTO.setStatus("fail");
            ajaxResult.error(defaultMsg);
        }
        result.setContent(resultDTO);
        resultDTO.setData(ajaxResult);

        return AesCryptorUtils.encrypt(JSONUtil.toJsonStr(result), AesCryptorUtils.SECURITY_KEY);
    }
}
